<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>腾飞健康管理平台</title>
    <link rel="stylesheet" href="lib/layui/css/layui.css">
</head>
<body>

<div style="padding: 10px">
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" lay-verify="required" name="examinerId" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" lay-verify="required" name="examinerName" disabled>
            </div>
        </div>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>角色</legend>
        </fieldset>
        <div class="layui-form-item" name="roles"></div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="submitRole">立即提交</button>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript" src="lib/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(["form", "jquery", "layer"], function () {
        let $ = layui.jquery;
        let layer = layui.layer;
        let form = layui.form;


        //获取父页面传递的参数
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r !== null)
                return unescape(r[2]);
            return null;
        };

        $(document).ready(function () {
            let examinerId = getQueryString("examinerId");
            $("input[name=examinerId]").val(examinerId);
            let loadIndex = null;
            $.ajax({
                url: "/getRolesByExaminerId",
                dataType: "json",
                type: "get",
                data: "examinerId=" + examinerId,
                beforeSend: function () {
                    loadIndex = layer.load();
                },
                success: function (responseData) {
                    layer.close(loadIndex);
                    let roles;
                    let examinerRoleIds;
                    for (var key in responseData) {
                        if(key== "roles"){
                            roles=responseData[key];
                        }else if(key == "examinerRoleIds"){
                            examinerRoleIds=responseData[key];
                        }else if(key == "examiner"){
                            $("input[name=examinerName]").val(responseData[key].examinerName);
                        }
                    }
                    $("div[name=roles]").html("");
                    for(let i=0; i<roles.length; i++){
                        let flag=false;
                        for(let j=0; j<examinerRoleIds.length; j++){
                            if(examinerRoleIds[j]==roles[i].roleId)
                                flag=true;
                        }
                        if(flag==true){
                            $("div[name=roles]").append("<input type='checkbox' name='" + roles[i].role + "' value='" + roles[i].roleId + "' title='" + roles[i].roleName + "' checked=''>");
                        }else{
                            $("div[name=roles]").append("<input type='checkbox' name='" + roles[i].role + "' value='" + roles[i].roleId + "' title='" + roles[i].roleName + "' >");
                        }
                    }
                    form.render();
                },
                error: function () {
                    layer.close(loadIndex);
                    layer.msg("服务器错误，请重试！", {icon: 2, time: 1000});
                },
            });
        });

        form.on("submit(submitRole)", function (data) {
            console.log(data.field);
            let roleIds="";
            for(let key in data.field) {
                if(key!="examinerId" && key!="examinerName"){
                    roleIds=roleIds+(data.field)[key]+",";
                }
            }

            $.ajax({
                url: "/executeApportionRole",
                data: "examinerId="+data.field.examinerId+"&roleIds="+roleIds,
                dataType: "json",
                type: "post",
                success: function (responseResult) {
                    if (responseResult.code == 1) {
                        layer.msg(responseResult.msg, {icon: 1, time: 1000});
                    } else {
                        layer.msg(responseResult.msg, {icon: 7, time: 1000});
                    }
                },
                error: function () {
                    layer.msg("服务器错误，请重试！", {icon: 2, time: 1000});
                }
            });
            return false;
        });
    });
</script>
</body>
</html>